package com.corpgovernment.common.utils;


import com.ctrip.corp.obt.generic.utils.StringUtils;

public class SecurityUtil {


    public static String maskNumber(String number, SensitiveType typeEnum) {
        if (StringUtils.isEmpty(number)) {
            return number;
        }

        switch (typeEnum) {
            case EMAIL: {
                String prefix = number.substring(0, number.indexOf("@"));
                if (prefix.length() == 2) {
                    StringBuffer sb = new StringBuffer(number);
                    sb.replace(1, 2, "*");
                    number = sb.toString();
                } else {
                    prefix = prefix.replaceAll("(?<=\\w{1})\\w(?=\\w{1})", "*");
                    String suffix = number.substring(number.indexOf("@"));
                    number = prefix + suffix;
                }
            }
            break;
            case PHONE: {
                if (number.startsWith("+86 ")) {
                    String suffix = number.substring(4);
                    number = "+86 " + suffix.replaceAll("(?<=\\w{3})\\w(?=\\w{4})", "*");
                } else if (number.startsWith("+86")) {
                    String suffix = number.substring(3);
                    number = "+86 " + suffix.replaceAll("(?<=\\w{3})\\w(?=\\w{4})", "*");
                } else {
                    number = number.replaceAll("(?<=\\w{3})\\w(?=\\w{4})", "*");
                }
            }
            break;
            case IDCARD: {
                number = number.replaceAll("(?<=\\w{4})\\w(?=\\w{4})", "*");
            }
            break;
            case ORDERCARD: {
                if (number.length() < 4) {
                    break;
                } else {
                    Double ceil = Math.ceil(number.length() / 3D);
                    StringBuffer sb = new StringBuffer(number);
                    for (int i = ceil.intValue(); i < number.length() - 2; i++) {
                        sb.replace(i, i + 1, "*");
                    }
                    number = sb.toString();
                }
            }
            break;
            default: {
            }
            break;
        }
        return number;
    }

}
